package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.example.annotation.Log;
import org.example.domain.po.ErpMaterialCategory;
import org.example.domain.po.ErpOrganization;
import org.example.domain.po.ErpUser;
import org.example.domain.po.R;
import org.example.domain.vo.ErpMaterialCategoryVo;
import org.example.domain.vo.ErpOrganizationVO;
import org.example.enums.BusinessType;
import org.example.service.ErpMaterialCategoryService;
import org.example.service.ErpOrganizationService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cam")
@Api("/商品类别")
public class ErpMaterialCategoryController {
    @Resource
    private ErpMaterialCategoryService categoryService;

    @GetMapping("/findOrganTree")

    public Result<List<ErpMaterialCategoryVo>> findAll() {
        return Result.success(categoryService.selectOrganVOTree());
    }

    @GetMapping("/queryParent")

    public Result<List<ErpMaterialCategoryVo>> queryParent(Long id, @RequestAttribute ErpUser user) {
        return Result.success(categoryService.queryParent(id, 0L, user.getTenantId()));
    }


    @PutMapping("/upOrgan")

    public Result<String> update(@RequestBody ErpMaterialCategory organClick) {
        boolean b = categoryService.updateOrgan(organClick);
        return b ? Result.success("操作成功") : Result.error("更新失败") ;
    }


    @DeleteMapping("/batchRemoveOrgan/{organAbs}")

    public Result<String> remove(@PathVariable List<String> organAbs, @RequestAttribute ErpUser user){
        boolean b = categoryService.removeBatchByOrgAbs(organAbs, user.getTenantId());
        return b ? Result.success("操作成功！") : Result.error("失败");
    }


    @PostMapping("/organInsert")

    public Result<String> save(@RequestBody ErpMaterialCategory organClick, @RequestAttribute("user") ErpUser user) {
        organClick.setTenantId(user.getTenantId());
        boolean save = categoryService.organInsert(organClick);
        return save ? Result.success("操作成功") : Result.error("失败");
    }

    @GetMapping("/findOrgOne")
    public Result<Boolean> findOrgOne(String orgAbr, @RequestAttribute ErpUser user){
        LambdaQueryWrapper<ErpMaterialCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ErpMaterialCategory::getName, orgAbr)
                .eq(ErpMaterialCategory::getTenantId, user.getTenantId());
        long count = categoryService.count(wrapper);
        boolean h = count == 0;
        return Result.success(h);
    }
    
    @GetMapping("findCategoryByTenantId")
    public Result<List<ErpMaterialCategoryVo>> findCategoryByTenantId(@RequestAttribute ErpUser user) {
        
        return Result.success(categoryService.findCategoryByTenantId(user.getTenantId()));
    }
    
    
}
